package com.interview.algorithm;

import java.util.HashSet;
import java.util.Set;

/**
 * Copyright (C), 2018-2020
 * FileName: 摩斯码
 * Author:   kongfanyu
 * Date:     2020/12/30 下午7:33
 */
public class 摩斯码 {
    public static void main(String[] args) {
        String[] words = {"gin", "zen", "gig", "msg"};
        int i = uniqueMorseRepresentations(words);
        System.out.println(i);
    }

    public static int uniqueMorseRepresentations(String[] words) {
        String[] codes = new String[]{".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
                "....", "..", ".---", "-.-", ".-..", "--", "-.",
                "---", ".--.", "--.-", ".-.", "...", "-", "..-",
                "...-", ".--", "-..-", "-.--", "--.."};

        Set<String> seen = new HashSet();
        for (String word : words) {
            StringBuilder code = new StringBuilder();
            for (char c : word.toCharArray()) { //将字符中的数据以字符数组的形式返回
                code.append(codes[c - 'a']);
            }
            seen.add(code.toString());
        }

        return seen.size();
    }
}
